<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="test"></div>
    <script src="../lib2/react.development.js"></script>
    <!-- //引入核心库 -->
    <script src="../lib2//react-dom.development.js"></script>
    <!-- //支持react的dom操作 -->
    <script src="../lib/babel.min.js"></script>
    <script type="text/babel">
        const MyContext = React.createContext()
        const { Provider, Consumer } = MyContext//consumer是给函数式组件提供的,provider是提供给类组件,该通信方式用的少，一般用于封装插件
        class A extends React.Component {
            state = { username: '我是a组件' }
            componentDidMount() {
                console.log(this)
            }
            render() {
                return (<div><p>使用context进行通信,我是A组件</p>
                    <Provider value={this.state.username}>
                        <B />
                        <C />
                    </Provider>
                </div>)
            }
        }
        function B() {
            return <div>
                我是B组件我接受到的消息是:
                <Consumer>
                    {
                        value => {
                            console.log(value)
                            return `${value}`
                        }
                    }
                </Consumer>
            </div>
        }
        class C extends React.Component {
            static contextType = MyContext//声明接收
            render() {
                console.log(this.context)
                return (<div>我是C组件我接受到的信息是:{this.context}</div>)
            }
        }
        ReactDOM.render(<A />, document.getElementById('test'))
    </script>
</body>

</html>